launch('com.smile.gifmaker');
toast('打开快手')
// App加载完成
waitForPackage('com.smile.gifmaker');
toast('加载完成')
sleep(6000)

// 点击消息
const xxKS = className("androidx.appcompat.app.ActionBar$c").desc("消息").findOne(3000)
if(!xxKS){
    toast('没有消息')
}
xxKS.click()

const readPopup = className("android.widget.TextView").depth("13").drawingOrder("4").text("私信支持已读状态").findOne(2000)
if(readPopup){
    className("android.view.ViewGroup").depth("13").drawingOrder("3").findOne(2000).click()
}else{
    console.log('没找到');
}

// 查找新消息
// findNewMsgKS()
// // 评论回复
// commentKS()
// back()
// 快手截流
riverKS()

// 查找新消息
function findNewMsgKS() {
    const redXxKS = className("android.widget.FrameLayout").depth("19").drawingOrder("18").findOne(2000)
    console.log(redXxKS.bounds().width());
    if(!redXxKS||redXxKS.bounds().width()<1){
        // 没找到新消息
        toastLog('没有新消息')
        return
    }
    click(redXxKS.bounds().centerX(),redXxKS.bounds().centerY())
    sleep(random(1000,3000))
    // 加微信好友
    const sxTextListKS = className("android.widget.FrameLayout").depth("11").drawingOrder("8").find()
    console.log(sxTextListKS[sxTextListKS.length-1].child(0).text());
    console.log(extractWeChatID(sxTextListKS[sxTextListKS.length-1].child(0).text()));

    // 回复
    const inputKS = className("android.widget.EditText").depth("11").drawingOrder("2").text("输入消息").findOne(2000)
    if(!inputKS){
        console.log('没找到输入框');
    }
    inputKS.setText('1')
    className("android.widget.ImageView").depth("12").drawingOrder("2").findOne(2000).click()
    sleep(random(1000,3000))
    back()
    sleep(random(1000,3000))
    findNewMsgKS()
}


// 评论回复
function commentKS(){
    const redCom = className("android.widget.TextView").depth("18").drawingOrder("3").findOne(2000)
    if(!redCom){
        // 没找到新消息
        toastLog('没有评论')
        back()
        return
    }
    console.log(redCom.bounds().centerX(),redCom.bounds().centerY());
    click(redCom.bounds().centerX(),redCom.bounds().centerY())
    sleep(random(1000,3000))
    className("android.view.ViewGroup").depth("14").drawingOrder("3").findOne(2000).click()
    const unreadCom = className("android.view.ViewGroup").depth("22").drawingOrder("1").findOne(2000)
    const unreadComChildNum = unreadCom.parent().child(0).child(1).childCount()
    unreadCom.parent().child(0).child(1).child(unreadComChildNum-1).child(0).click()
    const comInputKS = className("android.widget.EditText").depth("9").drawingOrder("2").findOne(2000)
    if(!comInputKS){
        toastLog('没找到评论框')
    }
    comInputKS.setText('7')
    sleep(random(1000,3000))
    className("android.widget.Button").depth("8").desc("发送").text("发送").findOne(2000).click()
    sleep(random(1000,3000))
    commentKS()
}


// 快手截流
function riverKS(){
    const moreKS = className("android.widget.ImageView").desc("查找").findOne(2000)
    if(!moreKS){
        toastLog('没找到查找')
        return
    }
    moreKS.click()
    sleep(random(1000,3000))
    const arrKS = ['2375479254']
    arrKS.forEach(e=>{
        const inputUserKS = className("android.widget.EditText").depth("12").drawingOrder("2").findOne(2000)
    if(!inputUserKS){
        toastLog('没找到输入框')
        return
    }
    inputUserKS.setText(e)
    sleep(random(2000,3000))
    const searchKS = className("android.widget.TextView").depth("12").text("搜索").findOne(2000)
    if(!searchKS){
        toastLog('没找到搜索')
        return
    }
    searchKS.click()
    sleep(random(2000,3000))
    const userButtonKS = className("android.view.View").depth("12").drawingOrder("2").findOne(2000)
    if(!userButtonKS){
        toastLog('没找到用户')
        return
    }
    userButtonKS.click()
    sleep(random(2000,3000))
    const userKS = className("android.widget.LinearLayout").depth("17").drawingOrder("2").findOne(2000)
    if(!userKS){
        toastLog('没找到快手账号')
        return
    }
    click(userKS.bounds().centerX(),userKS.bounds().centerY())
    sleep(random(2000,3000))
    const zuoPinKS = className("android.widget.RelativeLayout").depth("9").drawingOrder("1").findOne(2000)
    if(!zuoPinKS){
        toastLog('没找到作品')
        return
    }
    zuoPinKS.click()
    threeKS()
    })
}


let FaBuTimeKS = 1
function threeKS() {
    sleep(random(1000,3000))
    let videoTimeKSList = className("android.widget.TextView").depth("13").drawingOrder("1").find()

    console.log(videoTimeKSList.length);
    videoTimeKSList = videoTimeKSList.filter(e=>{
        console.log(e.text());
        console.log(e.text().includes('发布'));
        return e.text().includes('发布于')
    })
    console.log(videoTimeKSList.length);
    let videoTimeKS = videoTimeKSList[0].text()
    console.log(videoTimeKS);
    videoTimeKS = videoTimeKS.split('·')
    console.log(videoTimeKS[0].trim());
    // 全屏
    const x = className("android.widget.FrameLayout").depth("0").drawingOrder("0").findOne(1000)
        if(videoTimeKS[0].trim() == FaBuTimeKS){
            toastLog('没有视频了')
            back()
            sleep(random(1000,3000))
            back()
            sleep(random(1000,3000))
            back()
            return
        }else{
            FaBuTimeKS = videoTimeKS[0].trim()
        }
        const pingLun = className("android.widget.FrameLayout").depth("12").drawingOrder("4").findOne(1000)
        if(pingLun){
            if(pingLun.child(3).text()=="抢首评"){
                toastLog('没有评论')
                swipe(
                    device.width / 2,
                    device.height * 0.8,
                    device.width / 2,
                    device.height * 0.2,
                    random(500,1000)
                )
                threeKS()
            }else{
                pingLun.click()
                jieLiuKS()
                threeKS()
            }
        }
}



function jieLiuKS(){
    const pingLunKS = className("android.view.ViewGroup").depth("15").find()
    pingLunKS = pingLunKS.filter(e=>e.childCount()>1)
    let arr = []
    pingLunKS.forEach(e=>{
      console.log(e.child(0).bounds().height());
      arr.push(e.child(0).bounds().height())
    })
    pingLunKS.forEach(e=>{
      if(e.child(0).bounds().height() < Math.max.apply(null, arr))return 
      if(e.childCount()!=7) return
        console.log(e.child(5).child(0).text());
        // if(e.child(4).child(0).text() == '10'){
        //     e.child(0).click()
        //     sleep(random(1000,3000))
        //     const faSiXinKS = className("android.widget.ImageView").depth("12").drawingOrder("4").findOne(2000)
        //     if(!faSiXinKS){
        //         toastLog('没找到发私信')
        //         return
        //     }
        //     faSiXinKS.click()
        //     const faSiXinInputKS = className("android.widget.EditText").depth("11").drawingOrder("2").text("输入消息").findOne(2000)
        //     faSiXinInputKS.setText('1')
        //     sleep(random(1000,3000))
        //     className("android.widget.ImageView").depth("12").drawingOrder("2").click()
        //     back()
        //     sleep(random(1000,2000))
        //     back()
        // }
        e.child(0).click()
        sleep(random(1000,3000))
        let KSHao = className("android.widget.TextView").depth("13").drawingOrder("1").find()
        let IPHao = className("android.widget.TextView").depth("13").drawingOrder("2").find()
        KSHao = KSHao.filter(e=>e.text().includes("快手号"))
        IPHao = IPHao.filter(e=>e.text().includes("IP"))
        console.log(KSHao[0].text());
        console.log(IPHao[0].text());
        back()
        sleep(random(1000,3000))
    })
    if(className("android.view.View").depth("17").drawingOrder("2").findOne(2000)){
        toast('没有评论了')
        back()
    }else{
        const PingLunBoxKS = className("androidx.recyclerview.widget.RecyclerView").depth("14").drawingOrder("2").findOne(2000)
        swipe(PingLunBoxKS.bounds().centerX(), PingLunBoxKS.bounds().centerY(),device.width/2,device.height*1/3,1000 )
        jieLiuKS()
    }
}



/**
 * 提取微信号
 *
 * @param str 包含微信号的字符串
 * @returns 返回匹配到的微信号，如果未匹配到则返回null
 */
function extractWeChatID(str) {
    const weChatIDRegex = /[a-zA-Z_][a-zA-Z0-9_-]{5,19}/;
    const match = str.match(weChatIDRegex);
    return match ? match[0] : null;
}

function random(min, max) {
    return Math.floor(Math.random() * (max - min + 1)) + min;
}

/**
 * 判断给定时间字符串是否在三天内
 *
 * @param timeStr 时间字符串，格式为"YYYY-MM-DD HH:MM"
 * @returns 返回布尔值，若在三天内返回true，否则返回false
 */
function isTimeWithinThreeDays(timeStr) {
    console.log(timeStr);
    // 当前时间
    const currentTime = new Date();
    const inputTime = new Date(timeStr.replace(' ', 'T') + ':00')
  
    // 如果解析失败，则返回false
    if (isNaN(inputTime.getTime())) {
      console.error("无效的时间格式");
      return false;
    }
  
    // 计算当前时间与目标时间之间的时间差（毫秒）
    const timeDifference = currentTime - inputTime;
  
    // 三天的时间量（毫秒）
    const threeDays = 3 * 24 * 60 * 60 * 1000;
  
    // 检查时间差是否在三天内
    if (timeDifference < threeDays) {
      return true; // 在三天内
    } else {
      return false; // 不在三天内
    }
  }